package com.ma.process.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ma.auth.service.SysUserService;
import com.ma.common.result.Result;
import com.ma.model.process.Process;
import com.ma.process.service.ProcessService;
import com.ma.vo.process.ApprovalVo;
import com.ma.vo.process.ProcessFormVo;
import com.ma.vo.process.ProcessQueryVo;
import com.ma.vo.process.ProcessVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api(tags = "审批流管理")
@RestController
@RequestMapping("/admin/process")
public class ProcessController {
    @Autowired
    private ProcessService processService;
    @Autowired
    private SysUserService userService;

    //获取分页列表
    @ApiOperation("获取分页列表")
    @GetMapping("{page}/{limit}")
    public Result index(@PathVariable Long page, @PathVariable Long limit, ProcessQueryVo processQueryVo){
        Page<ProcessVo> voPage = processService.index(page,limit,processQueryVo);
        return Result.ok(voPage);
    }
    @ApiOperation("启动流程")
    @PostMapping("/startUp")
    public Result startUp(@RequestBody ProcessFormVo processFormVo){
        processService.startUp(processFormVo);
        return Result.ok();
    }
    @ApiOperation("查找当前用户待处理任务")
    @GetMapping("/findPending/{page}/{limit}")
    public Result findPending(@PathVariable Long page,@PathVariable Long limit){
        return Result.ok(processService.findPending(page,limit));
    }
    @ApiOperation("查看审批详细信息")
    @GetMapping("show/{id}")
    public Result show(@PathVariable Long id){
        Map<String,Object> map = processService.show(id);
        return Result.ok(map);
    }
    @ApiOperation(value = "审批")
    @PostMapping("approve")
    public Result approve(@RequestBody ApprovalVo approvalVo) {
        processService.approve(approvalVo);
        return Result.ok();
    }
    @ApiOperation(value = "已处理")
    @GetMapping("/findProcessed/{page}/{limit}")
    public Result findProcessed(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {
        Page<Process> pageParam = new Page<>(page, limit);
        return Result.ok(processService.findProcessed(pageParam));
    }
    @ApiOperation(value = "已发起")
    @GetMapping("/findStarted/{page}/{limit}")
    public Result findStarted(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {
        Page<ProcessVo> pageParam = new Page<>(page, limit);
        return Result.ok(processService.findStarted(pageParam));
    }
    @ApiOperation(value = "获取当前用户基本信息")
    @GetMapping("getCurrentUser")
    public Result getCurrentUser() {
        return Result.ok(userService.getCurrentUser());
    }
}
